 //2181.合并零之间的节点
 //https://leetcode.cn/problems/merge-nodes-in-between-zeros/
class Solution {
public:
    ListNode* mergeNodes(ListNode* head) {
        if(!head) return head;
        ListNode* cur = head;
        while(cur)
        {
            //不使用delete后效率变高
            if(cur->val == 0)
            {
                ListNode* tail = cur->next;
                while(tail && tail->val)
                {
                    cur->val += tail->val;
                    ListNode* next = tail->next;
                    //delete tail;
                    tail = next;
                }
                if(tail && tail->next == nullptr && tail->val == 0)
                {
                    cur->next = nullptr;
                    //delete tail;
                }
                else
                {
                    cur->next = tail;
                    cur = cur->next;
                }
            }
            else cur = cur->next;
        }
        
        return head;
    }
};